package com.zx.system.block.mapstruct;

import com.zx.core.context.TenantContextHolder;
import com.zx.core.enums.StatusEnum;
import com.zx.system.block.model.SysPost;
import com.zx.system.block.param.PostParam;
import com.zx.system.block.vo.SysPostVO;
import org.mapstruct.AfterMapping;
import org.mapstruct.Mapper;
import org.mapstruct.MappingTarget;
import org.mapstruct.ReportingPolicy;
import org.mapstruct.factory.Mappers;

/**
 * <p>
 * description: 岗位参数映射 <br>
 * create: 2024-10-29 15:10
 * </p>
 *
 * @author zhou  xun
 */
@Mapper(unmappedTargetPolicy = ReportingPolicy.IGNORE)
public interface PostMapStructMapper {
    PostMapStructMapper INSTANCE = Mappers.getMapper(PostMapStructMapper.class);

    /**
     * 将PostParam转换为SysPost实体
     *
     * @param postParam 岗位参数
     * @return SysPost实体
     */
    SysPost postParamToSysPost(PostParam postParam);

    /**
     * 将SysPost实体转换为SysPostVO视图对象
     *
     * @param sysPost 岗位实体
     * @return SysPostVO视图对象
     */
    SysPostVO sysPostToSysPostVO(SysPost sysPost);

    /**
     * 在映射完成后，设置描述性字段
     *
     * @param sysPost   岗位实体
     * @param sysPostVO 岗位VO
     */
    @AfterMapping
    default void setDescriptions(SysPost sysPost, @MappingTarget SysPostVO sysPostVO) {
        // 设置状态描述
        Integer status = sysPost.getStatus();
        StatusEnum statusEnum = StatusEnum.getEnumByCode(status);
        if (statusEnum != null) {
            sysPostVO.setStatusDesc(statusEnum.getDesc());
        }
    }

    /**
     * 在映射完成后，设置租户名
     *
     * @param sysPost   岗位实体
     * @param sysPostVO 岗位VO
     */
    @AfterMapping
    default void setTenantName(SysPost sysPost, @MappingTarget SysPostVO sysPostVO) {
        sysPostVO.setTenantName(TenantContextHolder.getTenantName());
    }
}